<?php
/**
 *
 *
 * @category     Shuxun Cms
 * @date         2015年10月4日
 * @author       Shuxun Team <kaiping.jiang@scshux.com>
 * @file         Helper.php
 */
namespace  Scshux\Utils;
class Helper
{

	/**
	 * 获取ip
	 * @return Ambigous <string, unknown>
	 */
	public  static   function getIP()
	{
		if (isset($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"])&&!empty($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"]))
		{
			$ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
		}
		elseif (isset($HTTP_SERVER_VARS["HTTP_CLIENT_IP"])&&!empty($HTTP_SERVER_VARS["HTTP_CLIENT_IP"]))
		{
			$ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];
		}
		elseif (isset($HTTP_SERVER_VARS["REMOTE_ADDR"])&&!empty($HTTP_SERVER_VARS["REMOTE_ADDR"]))
		{
			$ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];
		}
		elseif (getenv("HTTP_X_FORWARDED_FOR"))
		{
			$ip = getenv("HTTP_X_FORWARDED_FOR");
		}
		elseif (getenv("HTTP_CLIENT_IP"))
		{
			$ip = getenv("HTTP_CLIENT_IP");
		}
		elseif (getenv("REMOTE_ADDR"))
		{
			$ip = getenv("REMOTE_ADDR");
		}
		elseif (isset($_SERVER['REMOTE_ADDR'])){
			$ip = $_SERVER['REMOTE_ADDR'];
		}
		else
		{
			$ip = "Unknown";
		}
		return  $ip;

	}

	/**
	 * 获取当前浏览器
	 * @return
	 */
	public static   function  getUserAgent()
	{
		return $_SERVER['HTTP_USER_AGENT'];
	}


	/**
	 * http post
	 * @param string $url
	 * @param string|array $data
	 */
	public static   function  httpPost($url, $data, $timeout=40,$isJson=false)
	{

		if (is_array($data))
		{
			$data = http_build_query($data);
		}

		if (!function_exists("curl_init"))
		{
			die('undefined function curl_init');
		}

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT,$timeout);

		if($isJson){
			curl_setopt($ch, CURLOPT_HTTPHEADER, array(
			'Content-Type: application/json',
			'Content-Length: ' . strlen($data))
			);
		}

		/**************測試環境先不驗證ssl準確性**************/
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		/**************測試環境先不驗證ssl準確性**************/
		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0');
		$rs = curl_exec($ch);
		curl_close($ch);
		return $rs;
	}


	/**
	 * 转化时间
	 * @param  $timestamp
	 * @param  $granularity
	 * @return string
	 */
	public static function formatInterval($timestamp, $granularity = 5) {

		$units = array(
				//31536000 => '年',
				//604800 => '月',
				//86400 => '天',
				3600 =>'小时',
				60 => '分钟',
				1 => '秒'
		);

		$output = '';

		foreach ($units as $key => $value) {
			if ($timestamp >= $key) {
				$output .= floor($timestamp / $key) . $value;
				$timestamp %= $key;
				$granularity--;
			}
			if ($granularity == 0) {
				break;
			}
		}

		return $output ? $output : '0 秒';
	}


	/**
	 * http get
	 * @param string $url
	 * @param number $timeout
	 * @return mixed
	 */
	public static   function httpGet($url,$timeout=40)
	{

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0');

		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

		/**************測試環境先不驗證ssl準確性**************/
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		/**************測試環境先不驗證ssl準確性**************/

		$data = curl_exec($ch);
		curl_close($ch);
		return $data;
	}
	
	/**
	 * 
	 */

}